/**
 * 
 */
package net.sahv.bdyz.controller.admin;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import net.sahv.bdyz.model.Admin;
import net.sahv.bdyz.model.AdminRole;
import net.sahv.bdyz.service.AdminRoleService;
import net.sahv.bdyz.service.AdminService;
import net.sahv.bdyz.service.MemberLoginInfoService;
import net.sahv.bdyz.service.MemberService;
import net.sahv.bdyz.util.PagingUtil;

/**
 * @author 张天敏
 * @date 日期：2016年9月27日 时间：上午9:30:37
 */
@Controller("memberSortController")
@RequestMapping("admin/memberSort")
public class MemberSortController {
	
	@Autowired
	private MemberService memberService;
	@Autowired
	private AdminService adminService;
	@Autowired
	private AdminRoleService adminRoleService;// 角色管理员关系
	@Autowired
	private MemberLoginInfoService memberLoginInfoService;
	
	/**
	 * 跳转到会员访问排名列表（日）页面
	 * @author 张天敏
	 * @date 日期：2016年9月27日 时间：上午9:42:13
	 * @return
	 */
	@RequestMapping(value="/memberSortDayList",method=RequestMethod.GET)
	public String memberSortDayList(){
		return "admin/memberSort/memberSortDayList";
	}
	
	/**
	 * 会员访问排名列表（日）
	 * @author 张天敏
	 * @date 日期：2016年9月27日 时间：上午9:42:46
	 * @param request
	 * @param pagingUtil
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/memberSortDayList",method=RequestMethod.POST)
	public JSON memberSortDayList(HttpServletRequest request,PagingUtil pagingUtil){
		Admin admin = adminService.getCurrent();// 获取当前登录管理员
		Map<String, Object> param = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		if (admin != null) {
			AdminRole ar = adminRoleService.selectByAid(admin.getaId());
			int roleId = 0;
			if (ar != null) {
				roleId = ar.getrId();
			}
			String M_NICKNAME=request.getParameter("M_NICKNAME");
			String M_PHONE=request.getParameter("M_PHONE");
			
			String tableSql=" SELECT mli.`M_ID`,m.`M_NICKNAME`,m.`M_PHONE`,COUNT(m.`M_ID`) AS memberCount,DATE_FORMAT(mli.`MLI_CREATE_TIME`,'%Y-%m-%d') AS groupDate  FROM `member_login_info` mli LEFT JOIN `member` m ON mli.`M_ID`=m.`M_ID` ";

			String sortField = " GROUP BY m.`M_ID`,DATE_FORMAT(mli.`MLI_CREATE_TIME`,'%Y-%m-%d') ORDER BY COUNT(m.`M_ID`) DESC,DATE_FORMAT(mli.`MLI_CREATE_TIME`,'%Y-%m-%d') DESC ";
			String where = " where 1=1 ";
			
			if (roleId == 2) {
				where += " and M_MESSAGE_1=" + admin.getaId();
			}
			
			if(M_NICKNAME!=null&&!M_NICKNAME.trim().equals("")){
				where+=" and m.`M_NICKNAME` like '%"+M_NICKNAME+"%' ";
			}
			
			if(M_PHONE!=null&&!M_PHONE.trim().equals("")){
				where+=" and m.`M_PHONE` like '%"+M_PHONE+"%' ";
			}

			param.put("start", pagingUtil.getiDisplayStart());
			param.put("length", pagingUtil.getiDisplayLength());
			param.put("tableSql", tableSql);
			param.put("where", where);
			param.put("sortField", sortField);
			//会员访问排名列表（日）
			List<Map<String, Object>> memberSortDayList = memberLoginInfoService.memberSortDayList(param);
			//会员访问排名列表（日）统计
			Integer memberSortDayListCount = memberLoginInfoService.memberSortDayListCount(param);

			map.put("aaData", memberSortDayList);
			map.put("iTotalRecords", memberSortDayListCount);
			map.put("iTotalDisplayRecords", memberSortDayListCount);
			map.put("sEcho", pagingUtil.getsEcho());
		} else {
			map.put("aaData", "");
			// 查出来总共有多少条记录
			map.put("iTotalRecords", 0);
			map.put("iTotalDisplayRecords", 0);
			map.put("sEcho", pagingUtil.getsEcho());
		}
		return (JSON) JSON.toJSON(map);
	}
	
	/**
	 * 跳转到会员访问排名列表（日）详情页面
	 * @author 张天敏
	 * @date 日期：2016年9月27日 时间：下午2:30:36
	 * @return
	 */
	@RequestMapping(value="/memberSortDayInfoList",method=RequestMethod.GET)
	public String memberSortDayInfoList(Model model,HttpServletRequest request){
		String groupDate=request.getParameter("groupDate");
		model.addAttribute("groupDate", groupDate);
		return "admin/memberSort/memberSortDayInfoList";
	}
	
	/**
	 * 会员访问排名列表（日）详情
	 * @author 张天敏
	 * @date 日期：2016年9月27日 时间：下午3:21:38
	 * @param request
	 * @param pagingUtil
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/memberSortDayInfoList",method=RequestMethod.POST)
	public JSON memberSortDayInfoList(HttpServletRequest request,PagingUtil pagingUtil){
		Admin admin = adminService.getCurrent();// 获取当前登录管理员
		Map<String, Object> param = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		if (admin != null) {
			AdminRole ar = adminRoleService.selectByAid(admin.getaId());
			int roleId = 0;
			if (ar != null) {
				roleId = ar.getrId();
			}
			String M_NICKNAME=request.getParameter("M_NICKNAME");
			String M_PHONE=request.getParameter("M_PHONE");
			String groupDate=request.getParameter("groupDate");
			
			String tableSql=" SELECT mli.`M_ID`,m.`M_NICKNAME`,m.`M_PHONE`,COUNT(m.`M_ID`) AS memberCount,DATE_FORMAT(mli.`MLI_CREATE_TIME`,'%Y-%m-%d') AS groupDate  FROM `member_login_info` mli LEFT JOIN `member` m ON mli.`M_ID`=m.`M_ID` ";

			String sortField = " GROUP BY m.`M_ID`,DATE_FORMAT(mli.`MLI_CREATE_TIME`,'%Y-%m-%d') ORDER BY COUNT(m.`M_ID`) DESC,DATE_FORMAT(mli.`MLI_CREATE_TIME`,'%Y-%m-%d') DESC ";
			String where = " where 1=1 ";
			
			if (roleId == 2) {
				where += " and M_MESSAGE_1=" + admin.getaId();
			}
			
			if(M_NICKNAME!=null&&!M_NICKNAME.trim().equals("")){
				where+=" and m.`M_NICKNAME` like '%"+M_NICKNAME+"%' ";
			}
			
			if(M_PHONE!=null&&!M_PHONE.trim().equals("")){
				where+=" and m.`M_PHONE` like '%"+M_PHONE+"%' ";
			}
			
			if(groupDate!=null&&!groupDate.trim().equals("")){
				where+=" AND DATE_FORMAT(mli.`MLI_CREATE_TIME`,'%Y-%m-%d')='"+groupDate+"' ";
			}

			param.put("start", pagingUtil.getiDisplayStart());
			param.put("length", pagingUtil.getiDisplayLength());
			param.put("tableSql", tableSql);
			param.put("where", where);
			param.put("sortField", sortField);
			//会员访问排名列表（日）
			List<Map<String, Object>> memberSortDayList = memberLoginInfoService.memberSortDayList(param);
			//会员访问排名列表（日）统计
			Integer memberSortDayListCount = memberLoginInfoService.memberSortDayListCount(param);

			map.put("aaData", memberSortDayList);
			map.put("iTotalRecords", memberSortDayListCount);
			map.put("iTotalDisplayRecords", memberSortDayListCount);
			map.put("sEcho", pagingUtil.getsEcho());
		} else {
			map.put("aaData", "");
			// 查出来总共有多少条记录
			map.put("iTotalRecords", 0);
			map.put("iTotalDisplayRecords", 0);
			map.put("sEcho", pagingUtil.getsEcho());
		}
		return (JSON) JSON.toJSON(map);
	}
	
	/**
	 * 跳转到会员访问排名列表（周）页面
	 * @author 张天敏
	 * @date 日期：2016年9月27日 时间：下午3:21:46
	 * @return
	 */
	@RequestMapping(value="/memberSortWeekList",method=RequestMethod.GET)
	public String memberSortWeekList(){
		return "admin/memberSort/memberSortWeekList";
	}
	
	/**
	 * 会员访问排名列表（周）
	 * @author 张天敏
	 * @date 日期：2016年9月27日 时间：下午3:21:51
	 * @param request
	 * @param pagingUtil
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/memberSortWeekList",method=RequestMethod.POST)
	public JSON memberSortWeekList(HttpServletRequest request,PagingUtil pagingUtil){
		Admin admin = adminService.getCurrent();// 获取当前登录管理员
		Map<String, Object> param = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		if (admin != null) {
			AdminRole ar = adminRoleService.selectByAid(admin.getaId());
			int roleId = 0;
			if (ar != null) {
				roleId = ar.getrId();
			}
			String M_NICKNAME=request.getParameter("M_NICKNAME");
			String M_PHONE=request.getParameter("M_PHONE");
			String groupWeek=request.getParameter("groupWeek");
			
			String tableSql=" SELECT mli.`M_ID`,m.`M_NICKNAME`,m.`M_PHONE`,COUNT(m.`M_ID`) AS memberCount,YEARWEEK(mli.`MLI_CREATE_TIME`) AS groupWeek FROM `member_login_info` mli LEFT JOIN `member` m ON mli.`M_ID`=m.`M_ID` ";

			String sortField = " GROUP BY m.`M_ID`,YEARWEEK(mli.`MLI_CREATE_TIME`) ORDER BY COUNT(m.`M_ID`) DESC,YEARWEEK(mli.`MLI_CREATE_TIME`) DESC ";
			String where = " where 1=1 ";
			
			if (roleId == 2) {
				where += " and M_MESSAGE_1=" + admin.getaId();
			}
			
			if(M_NICKNAME!=null&&!M_NICKNAME.trim().equals("")){
				where+=" and m.`M_NICKNAME` like '%"+M_NICKNAME+"%' ";
			}
			
			if(M_PHONE!=null&&!M_PHONE.trim().equals("")){
				where+=" and m.`M_PHONE` like '%"+M_PHONE+"%' ";
			}
			
			if(groupWeek!=null&&!groupWeek.trim().equals("")){
				where+=" AND WEEK(mli.`MLI_CREATE_TIME`)='"+groupWeek+"' ";
			}

			param.put("start", pagingUtil.getiDisplayStart());
			param.put("length", pagingUtil.getiDisplayLength());
			param.put("tableSql", tableSql);
			param.put("where", where);
			param.put("sortField", sortField);
			//会员访问排名列表（日）
			List<Map<String, Object>> memberSortDayList = memberLoginInfoService.memberSortDayList(param);
			//会员访问排名列表（日）统计
			Integer memberSortDayListCount = memberLoginInfoService.memberSortDayListCount(param);

			map.put("aaData", memberSortDayList);
			map.put("iTotalRecords", memberSortDayListCount);
			map.put("iTotalDisplayRecords", memberSortDayListCount);
			map.put("sEcho", pagingUtil.getsEcho());
		} else {
			map.put("aaData", "");
			// 查出来总共有多少条记录
			map.put("iTotalRecords", 0);
			map.put("iTotalDisplayRecords", 0);
			map.put("sEcho", pagingUtil.getsEcho());
		}
		return (JSON) JSON.toJSON(map);
	}
	
	/**
	 * 跳转到会员访问排名列表（周）详情页面
	 * @author 张天敏
	 * @date 日期：2016年9月27日 时间：下午4:01:27
	 * @return
	 */
	@RequestMapping(value="/memberSortWeekInfoList",method=RequestMethod.GET)
	public String memberSortWeekInfoList(Model model,HttpServletRequest request){
		String groupWeek=request.getParameter("groupWeek");
		model.addAttribute("groupWeek", groupWeek);
		return "admin/memberSort/memberSortWeekInfoList";
	}
	
	/**
	 * 会员访问排名列表（周）详情
	 * @author 张天敏
	 * @date 日期：2016年9月27日 时间：下午3:21:51
	 * @param request
	 * @param pagingUtil
	 * @return
	 * @throws ParseException 
	 */
	@ResponseBody
	@RequestMapping(value="/memberSortWeekInfoList",method=RequestMethod.POST)
	public JSON memberSortWeekInfoList(HttpServletRequest request,PagingUtil pagingUtil) throws ParseException{
		Admin admin = adminService.getCurrent();// 获取当前登录管理员
		Map<String, Object> param = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		if (admin != null) {
			AdminRole ar = adminRoleService.selectByAid(admin.getaId());
			int roleId = 0;
			if (ar != null) {
				roleId = ar.getrId();
			}
			String M_NICKNAME=request.getParameter("M_NICKNAME");
			String M_PHONE=request.getParameter("M_PHONE");
			String groupWeek=request.getParameter("groupWeek");
			
			String tableSql=" SELECT mli.`M_ID`,m.`M_NICKNAME`,m.`M_PHONE`,COUNT(m.`M_ID`) AS memberCount,YEARWEEK(mli.`MLI_CREATE_TIME`) AS groupWeek FROM `member_login_info` mli LEFT JOIN `member` m ON mli.`M_ID`=m.`M_ID` ";

			String sortField = " GROUP BY m.`M_ID`,YEARWEEK(mli.`MLI_CREATE_TIME`) ORDER BY COUNT(m.`M_ID`) DESC,YEARWEEK(mli.`MLI_CREATE_TIME`) DESC ";
			String where = " where 1=1 ";
			
			if (roleId == 2) {
				where += " and M_MESSAGE_1=" + admin.getaId();
			}
			
			if(M_NICKNAME!=null&&!M_NICKNAME.trim().equals("")){
				where+=" and m.`M_NICKNAME` like '%"+M_NICKNAME+"%' ";
			}
			
			if(M_PHONE!=null&&!M_PHONE.trim().equals("")){
				where+=" and m.`M_PHONE` like '%"+M_PHONE+"%' ";
			}
			
			if(groupWeek!=null&&!groupWeek.trim().equals("")){
				String groupWeekString=parseDateToWeek(groupWeek);
				where+=" AND YEARWEEK(mli.`MLI_CREATE_TIME`)='"+groupWeekString+"' ";
			}

			param.put("start", pagingUtil.getiDisplayStart());
			param.put("length", pagingUtil.getiDisplayLength());
			param.put("tableSql", tableSql);
			param.put("where", where);
			param.put("sortField", sortField);
			//会员访问排名列表（日）
			List<Map<String, Object>> memberSortDayList = memberLoginInfoService.memberSortDayList(param);
			//会员访问排名列表（日）统计
			Integer memberSortDayListCount = memberLoginInfoService.memberSortDayListCount(param);

			map.put("aaData", memberSortDayList);
			map.put("iTotalRecords", memberSortDayListCount);
			map.put("iTotalDisplayRecords", memberSortDayListCount);
			map.put("sEcho", pagingUtil.getsEcho());
		} else {
			map.put("aaData", "");
			// 查出来总共有多少条记录
			map.put("iTotalRecords", 0);
			map.put("iTotalDisplayRecords", 0);
			map.put("sEcho", pagingUtil.getsEcho());
		}
		return (JSON) JSON.toJSON(map);
	}
	
	/**
	 * 将查询日期转换成周
	 * @author 张天敏
	 * @date 日期：2016年9月27日 时间：下午4:25:32
	 * @param dateString
	 * @return
	 * @throws ParseException
	 */
	public static String parseDateToWeek(String dateString) throws ParseException{
		String[] dateStringArray=dateString.split("-");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		//Date date=sdf.parse("2016-09-21");
		Date date=sdf.parse(dateString);
		Calendar c = new GregorianCalendar(); 
		c.setFirstDayOfWeek(Calendar.MONDAY); 
		c.setMinimalDaysInFirstWeek(7); 
		c.setTime (date);
		System.out.println(c.get(Calendar.WEEK_OF_YEAR));
		String weekString=String.valueOf(c.get(Calendar.WEEK_OF_YEAR));
		System.out.println(dateStringArray[0]+weekString);
		return dateStringArray[0]+weekString;
	}
	
	/**
	 * 跳转到会员访问排名列表（月）页面
	 * @author 张天敏
	 * @date 日期：2016年9月27日 时间：下午4:42:57
	 * @return
	 */
	@RequestMapping(value="/memberSortMonthList",method=RequestMethod.GET)
	public String memberSortMonthList(){
		return "admin/memberSort/memberSortMonthList";
	}
	
	/**
	 * 会员访问排名列表（月）
	 * @author 张天敏
	 * @date 日期：2016年9月27日 时间：下午3:21:51
	 * @param request
	 * @param pagingUtil
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/memberSortMonthList",method=RequestMethod.POST)
	public JSON memberSortMonthList(HttpServletRequest request,PagingUtil pagingUtil){
		Admin admin = adminService.getCurrent();// 获取当前登录管理员
		Map<String, Object> param = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		if (admin != null) {
			AdminRole ar = adminRoleService.selectByAid(admin.getaId());
			int roleId = 0;
			if (ar != null) {
				roleId = ar.getrId();
			}
			String M_NICKNAME=request.getParameter("M_NICKNAME");
			String M_PHONE=request.getParameter("M_PHONE");
			String groupMonth=request.getParameter("groupMonth");
			
			String tableSql=" SELECT mli.`M_ID`,m.`M_NICKNAME`,m.`M_PHONE`,COUNT(m.`M_ID`) AS memberCount,DATE_FORMAT(mli.`MLI_CREATE_TIME`,'%Y-%m') AS groupMonth FROM `member_login_info` mli LEFT JOIN `member` m ON mli.`M_ID`=m.`M_ID` ";

			String sortField = " GROUP BY m.`M_ID`,DATE_FORMAT(mli.`MLI_CREATE_TIME`,'%Y-%m') ORDER BY COUNT(m.`M_ID`) DESC,DATE_FORMAT(mli.`MLI_CREATE_TIME`,'%Y-%m') DESC ";
			String where = " where 1=1 ";
			
			if (roleId == 2) {
				where += " and M_MESSAGE_1=" + admin.getaId();
			}
			
			if(M_NICKNAME!=null&&!M_NICKNAME.trim().equals("")){
				where+=" and m.`M_NICKNAME` like '%"+M_NICKNAME+"%' ";
			}
			
			if(M_PHONE!=null&&!M_PHONE.trim().equals("")){
				where+=" and m.`M_PHONE` like '%"+M_PHONE+"%' ";
			}
			
			if(groupMonth!=null&&!groupMonth.trim().equals("")){
				where+=" AND DATE_FORMAT(mli.`MLI_CREATE_TIME`,'%Y-%m')='"+groupMonth+"' ";
			}

			param.put("start", pagingUtil.getiDisplayStart());
			param.put("length", pagingUtil.getiDisplayLength());
			param.put("tableSql", tableSql);
			param.put("where", where);
			param.put("sortField", sortField);
			//会员访问排名列表（日）
			List<Map<String, Object>> memberSortDayList = memberLoginInfoService.memberSortDayList(param);
			//会员访问排名列表（日）统计
			Integer memberSortDayListCount = memberLoginInfoService.memberSortDayListCount(param);

			map.put("aaData", memberSortDayList);
			map.put("iTotalRecords", memberSortDayListCount);
			map.put("iTotalDisplayRecords", memberSortDayListCount);
			map.put("sEcho", pagingUtil.getsEcho());
		} else {
			map.put("aaData", "");
			// 查出来总共有多少条记录
			map.put("iTotalRecords", 0);
			map.put("iTotalDisplayRecords", 0);
			map.put("sEcho", pagingUtil.getsEcho());
		}
		return (JSON) JSON.toJSON(map);
	}
	
	/**
	 * 跳转到会员访问排名列表（月）详情页面
	 * @author 张天敏
	 * @date 日期：2016年9月27日 时间：下午5:41:24
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/memberSortMonthInfoList",method=RequestMethod.GET)
	public String memberSortMonthInfoList(Model model,HttpServletRequest request){
		String groupMonth=request.getParameter("groupMonth");
		model.addAttribute("groupMonth", groupMonth);
		return "admin/memberSort/memberSortMonthInfoList";
	}
	
	/**
	 * 会员访问排名列表（月）详情
	 * @author 张天敏
	 * @date 日期：2016年9月27日 时间：下午5:41:31
	 * @param request
	 * @param pagingUtil
	 * @return
	 * @throws ParseException
	 */
	@ResponseBody
	@RequestMapping(value="/memberSortMonthInfoList",method=RequestMethod.POST)
	public JSON memberSortMonthInfoList(HttpServletRequest request,PagingUtil pagingUtil) throws ParseException{
		Admin admin = adminService.getCurrent();// 获取当前登录管理员
		Map<String, Object> param = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		if (admin != null) {
			AdminRole ar = adminRoleService.selectByAid(admin.getaId());
			int roleId = 0;
			if (ar != null) {
				roleId = ar.getrId();
			}
			String M_NICKNAME=request.getParameter("M_NICKNAME");
			String M_PHONE=request.getParameter("M_PHONE");
			String groupMonth=request.getParameter("groupMonth");
			
			String tableSql=" SELECT mli.`M_ID`,m.`M_NICKNAME`,m.`M_PHONE`,COUNT(m.`M_ID`) AS memberCount,DATE_FORMAT(mli.`MLI_CREATE_TIME`,'%Y-%m') AS groupMonth FROM `member_login_info` mli LEFT JOIN `member` m ON mli.`M_ID`=m.`M_ID` ";

			String sortField = " GROUP BY m.`M_ID`,DATE_FORMAT(mli.`MLI_CREATE_TIME`,'%Y-%m') ORDER BY COUNT(m.`M_ID`) DESC,DATE_FORMAT(mli.`MLI_CREATE_TIME`,'%Y-%m') DESC ";
			String where = " where 1=1 ";
			
			if (roleId == 2) {
				where += " and M_MESSAGE_1=" + admin.getaId();
			}
			
			if(M_NICKNAME!=null&&!M_NICKNAME.trim().equals("")){
				where+=" and m.`M_NICKNAME` like '%"+M_NICKNAME+"%' ";
			}
			
			if(M_PHONE!=null&&!M_PHONE.trim().equals("")){
				where+=" and m.`M_PHONE` like '%"+M_PHONE+"%' ";
			}
			
			if(groupMonth!=null&&!groupMonth.trim().equals("")){
				where+=" AND DATE_FORMAT(mli.`MLI_CREATE_TIME`,'%Y-%m')='"+groupMonth+"' ";
			}

			param.put("start", pagingUtil.getiDisplayStart());
			param.put("length", pagingUtil.getiDisplayLength());
			param.put("tableSql", tableSql);
			param.put("where", where);
			param.put("sortField", sortField);
			//会员访问排名列表（日）
			List<Map<String, Object>> memberSortDayList = memberLoginInfoService.memberSortDayList(param);
			//会员访问排名列表（日）统计
			Integer memberSortDayListCount = memberLoginInfoService.memberSortDayListCount(param);

			map.put("aaData", memberSortDayList);
			map.put("iTotalRecords", memberSortDayListCount);
			map.put("iTotalDisplayRecords", memberSortDayListCount);
			map.put("sEcho", pagingUtil.getsEcho());
		} else {
			map.put("aaData", "");
			// 查出来总共有多少条记录
			map.put("iTotalRecords", 0);
			map.put("iTotalDisplayRecords", 0);
			map.put("sEcho", pagingUtil.getsEcho());
		}
		return (JSON) JSON.toJSON(map);
	}
	
	
	
	/**
	 * 跳转到会员访问排名列表（所有）页面
	* @author  作者 : 周晓娜
	* @date 创建时间：2016年9月28日 上午11:06:16
	* @param
	* @parameter
	* @return
	 */
	@RequestMapping(value="/memberSortAllList",method=RequestMethod.GET)
	public String memberSortAllList(Model model){
		List<Map<String,Object>> yearList = memberLoginInfoService.getYearInfo();
		model.addAttribute("yearList", yearList);//获取年份
		return "admin/memberSort/memberSortAllList";
	}
	/**
	 * 跳转到会员访问排名列表（所有）详情页面
	* @author  作者 : 周晓娜
	* @date 创建时间：2016年9月28日 上午11:06:30
	* @param
	* @parameter
	* @return
	 */
	@RequestMapping(value="/memberSortAllList1",method=RequestMethod.GET)
	public String memberSortAllList1(Model model,HttpServletRequest request){
		String groupDate=request.getParameter("groupDate");
		model.addAttribute("groupDate", groupDate);
		return "admin/memberSort/memberSortAllInfoList";
	}
	
	/**
	 * 会员访问排名列表（所有）详情
	* @author  作者 : 周晓娜
	* @date 创建时间：2016年9月28日 上午11:07:14
	* @param
	* @parameter
	* @return
	 */
	@ResponseBody
	@RequestMapping(value="/memberSortAllList",method=RequestMethod.POST)
	public JSON memberSortAllList(HttpServletRequest request,PagingUtil pagingUtil) throws ParseException{
		Admin admin = adminService.getCurrent();// 获取当前登录管理员
		Map<String, Object> param = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		if (admin != null) {
			AdminRole ar = adminRoleService.selectByAid(admin.getaId());
			int roleId = 0;
			if (ar != null) {
				roleId = ar.getrId();
			}
			String M_NICKNAME=request.getParameter("M_NICKNAME");
			String M_PHONE=request.getParameter("M_PHONE");
			String groupMonth=request.getParameter("groupDate");
			
			String tableSql=" SELECT mli.`M_ID`,m.`M_NICKNAME`,m.`M_PHONE`,COUNT(m.`M_ID`) AS memberCount,DATE_FORMAT(mli.`MLI_CREATE_TIME`,'%Y') AS groupAll FROM `member_login_info` mli LEFT JOIN `member` m ON mli.`M_ID`=m.`M_ID` ";
			//总的访问排名，没有必要按照时间进行分组
			String sortField = " GROUP BY m.`M_ID` ORDER BY COUNT(m.`M_ID`) DESC,DATE_FORMAT(mli.`MLI_CREATE_TIME`,'%Y') DESC ";
			String where = " where 1=1 ";
			
			if (roleId == 2) {
				where += " and M_MESSAGE_1=" + admin.getaId();
			}
			
			if(M_NICKNAME!=null&&!M_NICKNAME.trim().equals("")){
				where+=" and m.`M_NICKNAME` like '%"+M_NICKNAME+"%' ";
			}
			
			if(M_PHONE!=null&&!M_PHONE.trim().equals("")){
				where+=" and m.`M_PHONE` like '%"+M_PHONE+"%' ";
			}
			
			if(groupMonth!=null&&!groupMonth.trim().equals("")){
				where+=" AND DATE_FORMAT(mli.`MLI_CREATE_TIME`,'%Y')='"+groupMonth+"' ";
			}

			param.put("start", pagingUtil.getiDisplayStart());
			param.put("length", pagingUtil.getiDisplayLength());
			param.put("tableSql", tableSql);
			param.put("where", where);
			param.put("sortField", sortField);
			//会员访问排名列表（日）
			List<Map<String, Object>> memberSortDayList = memberLoginInfoService.memberSortDayList(param);
			//会员访问排名列表（日）统计
			Integer memberSortDayListCount = memberLoginInfoService.memberSortDayListCount(param);

			map.put("aaData", memberSortDayList);
			map.put("iTotalRecords", memberSortDayListCount);
			map.put("iTotalDisplayRecords", memberSortDayListCount);
			map.put("sEcho", pagingUtil.getsEcho());
		} else {
			map.put("aaData", "");
			// 查出来总共有多少条记录
			map.put("iTotalRecords", 0);
			map.put("iTotalDisplayRecords", 0);
			map.put("sEcho", pagingUtil.getsEcho());
		}
		return (JSON) JSON.toJSON(map);
	}
	
	/**
	 * 跳转到会员访问总量页面
	 * @author 张天敏
	 * @date 日期：2016年9月28日 时间：下午4:48:46
	 * @param model
	 * @param flag
	 * @return
	 */
	@RequestMapping(value="/memberDayList",method=RequestMethod.GET)
	public String memberDayList(Model model,String flag){
		model.addAttribute("flag", flag);
		return "admin/memberSort/memberDayList";
	}
	
	/**
	 * 会员访问总量
	 * @author 张天敏
	 * @date 日期：2016年9月27日 时间：上午9:42:46
	 * @param request
	 * @param pagingUtil
	 * @return
	 * @throws ParseException 
	 */
	@ResponseBody
	@RequestMapping(value="/memberDayList",method=RequestMethod.POST)
	public JSON memberDayList(HttpServletRequest request,PagingUtil pagingUtil) throws ParseException{
		Admin admin = adminService.getCurrent();// 获取当前登录管理员
		Map<String, Object> param = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		if (admin != null) {
			AdminRole ar = adminRoleService.selectByAid(admin.getaId());
			int roleId = 0;
			if (ar != null) {
				roleId = ar.getrId();
			}
			//flag：1、日；2、周；3、月
			String flag=request.getParameter("flag");
			
			String dateSql="";
			
			if(flag==null||flag.trim().equals("")){
				flag="1";
			}
			
			if(flag.equals("1")){
				dateSql=" DATE_FORMAT(mli.`MLI_CREATE_TIME`,'%Y-%m-%d') ";
			}else if(flag.equals("2")){
				dateSql=" YEARWEEK(mli.`MLI_CREATE_TIME`) ";
			}else if(flag.equals("3")){
				dateSql=" DATE_FORMAT(mli.`MLI_CREATE_TIME`,'%Y-%m') ";
			}
			
			String tableSql=" SELECT COUNT(*) AS memberCount,"+dateSql+" AS groupDate  FROM `member_login_info` mli LEFT JOIN `member` m ON mli.`M_ID`=m.`M_ID` ";

			String sortField = " GROUP BY "+dateSql+" ORDER BY "+dateSql+" DESC ";
			String where = " where 1=1 ";
			
			if (roleId == 2) {
				where += " and M_MESSAGE_1=" + admin.getaId();
			}
			
			param.put("start", pagingUtil.getiDisplayStart());
			param.put("length", pagingUtil.getiDisplayLength());
			param.put("tableSql", tableSql);
			param.put("where", where);
			param.put("sortField", sortField);
			//会员访问排名列表（日）
			List<Map<String, Object>> memberSortDayList = memberLoginInfoService.memberSortDayList(param);
			//会员访问排名列表（日）统计
			Integer memberSortDayListCount = memberLoginInfoService.memberSortDayListCount(param);

			map.put("aaData", memberSortDayList);
			map.put("iTotalRecords", memberSortDayListCount);
			map.put("iTotalDisplayRecords", memberSortDayListCount);
			map.put("sEcho", pagingUtil.getsEcho());
		} else {
			map.put("aaData", "");
			// 查出来总共有多少条记录
			map.put("iTotalRecords", 0);
			map.put("iTotalDisplayRecords", 0);
			map.put("sEcho", pagingUtil.getsEcho());
		}
		return (JSON) JSON.toJSON(map);
	}
	
	/**
	 * 跳转到下单总量页面
	 * @author 张天敏
	 * @date 日期：2016年9月28日 时间：下午4:50:29
	 * @param model
	 * @param flag
	 * @return
	 */
	@RequestMapping(value="/orderDayList",method=RequestMethod.GET)
	public String orderDayList(Model model,String flag){
		model.addAttribute("flag", flag);
		return "admin/memberSort/orderDayList";
	}
	
	/**
	 * 下单总量
	 * @author 张天敏
	 * @date 日期：2016年9月27日 时间：上午9:42:46
	 * @param request
	 * @param pagingUtil
	 * @return
	 * @throws ParseException 
	 */
	@ResponseBody
	@RequestMapping(value="/orderDayList",method=RequestMethod.POST)
	public JSON orderDayList(HttpServletRequest request,PagingUtil pagingUtil) throws ParseException{
		Admin admin = adminService.getCurrent();// 获取当前登录管理员
		Map<String, Object> param = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		if (admin != null) {
			AdminRole ar = adminRoleService.selectByAid(admin.getaId());
			int roleId = 0;
			if (ar != null) {
				roleId = ar.getrId();
			}
			//flag：1、日；2、周；3、月
			String flag=request.getParameter("flag");
			
			String dateSql="";
			
			if(flag==null||flag.trim().equals("")){
				flag="1";
			}
			
			if(flag.equals("1")){
				dateSql=" DATE_FORMAT(o.`O_CREAT_TIME`,'%Y-%m-%d') ";
			}else if(flag.equals("2")){
				dateSql=" YEARWEEK(o.`O_CREAT_TIME`) ";
			}else if(flag.equals("3")){
				dateSql=" DATE_FORMAT(o.`O_CREAT_TIME`,'%Y-%m') ";
			}
			
			String tableSql=" SELECT COUNT(*) AS memberCount,"+dateSql+" AS groupDate  FROM `orders` o LEFT JOIN member m ON o.`M_ID`=m.`M_ID` ";

			String sortField = " GROUP BY "+dateSql+" ORDER BY COUNT(*) DESC,"+dateSql+" DESC ";
			String where = " where 1=1 ";
			
			if (roleId == 2) {
				where += " and M_MESSAGE_1=" + admin.getaId();
			}
			
			param.put("start", pagingUtil.getiDisplayStart());
			param.put("length", pagingUtil.getiDisplayLength());
			param.put("tableSql", tableSql);
			param.put("where", where);
			param.put("sortField", sortField);
			//会员访问排名列表（日）
			List<Map<String, Object>> memberSortDayList = memberLoginInfoService.memberSortDayList(param);
			//会员访问排名列表（日）统计
			Integer memberSortDayListCount = memberLoginInfoService.memberSortDayListCount(param);

			map.put("aaData", memberSortDayList);
			map.put("iTotalRecords", memberSortDayListCount);
			map.put("iTotalDisplayRecords", memberSortDayListCount);
			map.put("sEcho", pagingUtil.getsEcho());
		} else {
			map.put("aaData", "");
			// 查出来总共有多少条记录
			map.put("iTotalRecords", 0);
			map.put("iTotalDisplayRecords", 0);
			map.put("sEcho", pagingUtil.getsEcho());
		}
		return (JSON) JSON.toJSON(map);
	}
	
	
}
